from wt.init_param.task_init import TaskInit
from wt.untils.common_until import CommonUntil
from wt.connector import workflow_func


class ProjectAPI(TaskInit):
    def __init__(self, res_db_log):
        super().__init__()
        self.common_until = CommonUntil(res_db_log)
        self.wt = workflow_func.WorkFlowExec(self.login_url, self.headers)

    def get_project_code(self, project_name):
        params = {
            "pageSize": 10,
            "pageNo": 1,
            "searchVal": project_name
        }
        res = self.wt.send_request(self.wt_projects_url, "get", params=params)
        tmp = self.common_until.get_res_json_value(res, "data")
        print(tmp)
        project_code = tmp["totalList"][0]["code"]
        return project_code


if __name__ == '__main__':
    res_db_log = open("project.log", 'wb+', buffering=0)
    p = ProjectAPI(res_db_log)
    p.get_project_code("26")
    # # import json
    # import json
    #
    #
    # def func(res, col):
    #     try:
    #         # 解析 JSON 数据
    #         data = json.loads(res)
    #         # 获取需要提取的字段值
    #         if isinstance(data, dict):
    #             value = data.get(col)
    #             if value is not None:
    #                 return value
    #             else:
    #                 for key in data.keys():
    #                     value = func(json.dumps(data[key]), col)
    #                     if value is not None:
    #                         return value
    #         elif isinstance(data, list):
    #             for item in data:
    #                 value = func(json.dumps(item), col)
    #                 if value is not None:
    #                     return value
    #     except Exception as e:
    #         print("Error:", e)
    #         return None
    #
    #
    # # 示例 JSON 数据
    # res = '{"code": 0, "msg": "成功", "exceptionStack": null, "data": {"totalList": [{"id": 73, "userId": 1, "userName": null, "code": 11187706993312, "name": "v26_回归项目_1"}]}}'
    #
    # # 调用函数提取字段值
    # result_name = func(res, 'name')
    # print(result_name)  # 输出: v26_回归项目_1
    #
    # result_msg = func(res, 'msg')
    # print(result_msg)  # 输出: 成功



